package com.gqzm.side.common.interceptor;

import com.gqzm.side.common.result.StringCamelUtils;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

/**
 * @ClassName: {@link ParameterNameRequestWrapper}
 * @description: 请求下划线转驼峰
 * @author: peter qin
 * @date: 2023 07 17
 * @version: 1.0
 **/
@Slf4j
public class ParameterNameRequestWrapper extends HttpServletRequestWrapper {
    private final Map<String, String[]> params = new HashMap<>();//定义参数集合

    public ParameterNameRequestWrapper(HttpServletRequest request) {
        super(request);
        request.getParameterMap()
                .forEach((k,v) -> params.put(StringCamelUtils.underlineToCamel(k), v));
    }

    @Override
    public String getParameter(String name) {
        String[] vs = params.get(StringCamelUtils.underlineToCamel(name));
        if(vs == null || vs.length <= 0) {
            return null;
        }
        return vs[0];
    }


    @Override
    public Enumeration getParameterNames() {
        return new Vector(params.keySet()).elements();
    }


    @Override
    public String[] getParameterValues(String name) {
        // 转为下划线参数
        return params.get(StringCamelUtils.underlineToCamel(name));
    }
}
